﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Agresso.Interface.Layer.Service;
using System.Collections;
using System.Configuration;
using Agresso.Interface.Layer.Domain;
using System.IO;

namespace Agresso.Interface.Layer.GUI
{
    class Program
    {
        static void Main(string[] args)// LIVE PRODUCTION
        {
          //  CrewPayrollService cps = new CrewPayrollService();
            //cps.updateNotExported();
           // cps.ExecuteCrewPayrollInterface(5, 2014, false, new string[]{"POSH XANADU"}, 201406, 201405, DateTime.Today, 203);
            //cps.e

            //VesselService vsvc = new VesselService();
          //  vsvc.UpdateVesselIDLLOYDNumber();

            //BunkerInventoryService bisvc = new BunkerInventoryService();
           // bisvc.UpdateEndVoyageDate();
           // bisvc.ImportAllDataSoftmarToMiddleLayerTotalConsumption();
//
   //         bisvc.ImportAllDataSoftmarToMiddleLayerMigration("1", "Glorious Sentosa", "Total Consumption");
         //   bisvc.UpdateVesselCode();

            //bisvc.ImportAllDataSoftmarToMiddleLayerBODandBOR();
           // bisvc.ImportAllDataSoftmarToMiddleLayerMigration("81", "ALAM PESONA", "Total Consumption");
            //bisvc.ExecuteInterfaceIWTotalConsumption();
            //bisvc.ExecuteInterfaceIWBunkerOnDelivery();
           // bisvc.ExecuteInterfaceIRBunkerOnReDelivery();
           // bisvc.ImportAllDataSoftmarToMiddleLayer();
            //ImportVoyageToProduction();
            // CheckAgressoProductionSupplier();
           

           // SupplierCustomerCheck();
            //CommissionInvoiceService cm = new CommissionInvoiceService();
           // cm.ImportAllDataSoftmarToMiddleLayer(SoftmarType.SOFTMAR_FREIGHT_HIRE_INVOICE, "PCLS/13509", true);
         //   cm.ImportAllDataSoftmarToMiddleLayer(SoftmarType.SOFTMAR_COMMISSION_INVOICE_TCRELET);
           // cm.ExecuteCommissionInvoiceTCReletTypeChargeDebit();
          //  cm.ImportAllDataSoftmarToMiddleLayer(SoftmarType.SOFTMAR_COMMISSION_INVOICE_TCRELET);
            //cm.UpdateNarrative();
           // cm.ExecuteCommissionInvoiceTCReletType();
           // cm.ExecuteCommissionInvoiceTCReletTypeChargeDebit();

           // cm.SetTestDateProforma(new DateTime(2013, 10, 2), new DateTime(2013, 9, 30));
           // cm.ExecuteInvoiceProformaCommissionCredit();
            //cm.ExecuteInvoiceProformaChargeCredit();
            //cm.ExecuteInvoiceProformaChargeDebit();

            //cm.ImportAllDataSoftmarToMiddleLayerProforma();
            //cm.ExecuteCommissionInvoiceTCReletTypeChargeDebit();
            //cm.ExecuteFreightHireInvoiceCargoReletCommissionCreditIN();
            //
            //cm.ExecuteFreightHireInvoiceOUTStatement();
            //cm.ImportAllDataSoftmarToMiddleLayer(SoftmarType.SOFTMAR_COMMISSION_INVOICE_TCRELET);
          // cm.ImportAllDataSoftmarToMiddleLayer(SoftmarType.SOFTMAR_FREIGHT_HIRE_INVOICE);
            //cm.ImportAllDataSoftmarToMiddleLayer(SoftmarType.SOFTMAR_PROFORMA);

            //StatementService stmtSVC = new StatementService();
          // stmtSVC.UpdateVesselsCode();
           // stmtSVC.ImportAllDataSoftmarToMiddleLayer();
            // stmtSVC.ExecuteStatementInterface();

           // BunkerPOService poSVC = new BunkerPOService();
           // poSVC.ExecuteBunkerProcurementPO();
          //  poSVC.UpdateApodettextOrderID();
           // poSVC.UpdateEstQtyMax();
        //    poSVC.UpdateVesselCode();
           // poSVC.ImportAllDataSoftmarToMiddleLayer(1123);
           // poSVC.ImportAllDataSoftmarToMiddleLayer();
          // poSVC.UpdateApodettextOrderID();
            //poSVC.ExecuteBunkerProcurementPO();

            //StatementService ssvc = new StatementService();
//
           // ssvc.UpdateFixedBy();
           // ssvc.ImportAllDataSoftmarToMiddleLayer(0);
           // ssvc.ImportAllDataSoftmarToMiddleLayer(1);
            //ssvc.ExecuteStatementInterface();

           //BunkerGRNService grnSvc = new BunkerGRNService();
            //grnSvc.ExecuteBunkerIRByPO(4500000002, 
          //  grnSvc.ImportAllDataSoftmarToMiddleLayer();
          // grnSvc.ExecuteBunkerGRNByPO();

           // VoyageMasterService vm = new VoyageMasterService();
          //  vm.UpdateVesselCode();
            //vm.ImportAllDataSoftmarToMiddleLayer();
          //  vm.ExecuteVoyageMasterInterface();
            //ImportVoyageToProduction();

            WinSDSService winsdsService = new WinSDSService();
            //winsdsService.UpdatePOIDMiddleLayer();
            winsdsService.UpdatePOApprovedDate();
            //winsdsService.UpdateAlgbatchinputRemark();
            //winsdsService.ImportSupplierDataFromAgressoToDMZ();
            //winsdsService.ExecuteInterfaceAllPOData();
            //winsdsService.UpdatePOID();
            //winsdsService.UpdatePOApprovedDate();

           // ImportBunkerToProduction();

            //ExecuteBunkerInventory();

           // ExecuteVoyageMasterNew();

           // StatmentUser();
        }

        private static void StatmentUser()
        {
            ResponsibleUserService user = new ResponsibleUserService();
            IList userStatement = new ArrayList();
            userStatement.Add("KENC");
            userStatement.Add("CUTE");
            userStatement.Add("SMAR");
            userStatement.Add("AKHA");
            userStatement.Add("PJK");
            userStatement.Add("RKUO");
            userStatement.Add("LIMH");
            userStatement.Add("ENB");
            userStatement.Add("mark");
            userStatement.Add("SCB");
            userStatement.Add("JWEE");
            userStatement.Add("ELB");
            userStatement.Add("HAMA");
            userStatement.Add("CHS");
            userStatement.Add("KLIM");
            userStatement.Add("SYED");
            userStatement.Add("STAN");
            userStatement.Add("GTEO");
            userStatement.Add("GRAM");
            userStatement.Add("ANNE");
            userStatement.Add("TIAN");
            userStatement.Add("SAMR");
            foreach (string p in userStatement)
            {
                user.GetResponsibleUserByName(p, "POSTFIX");
            }
        }

        private static void ExecuteVoyageMasterNew()
        {
            VesselService vs = new VesselService();
            VoyageMasterService vmSvc =new VoyageMasterService();


              StreamReader sr = new StreamReader(@"C:\Project\Temp\VoyageMasterAgresso.csv");
            int i = 0;
            while (!sr.EndOfStream)
            {
                String lines = sr.ReadLine();
                string[] line = lines.Split(',');
                string voyage = line[0];
                string dim_val = line[1];
                string vesselName = line[2];
                string client = line[3];

                Vessel vessel = vs.GetAllVesselMappingByDimValueAndClient(dim_val, client);
                if (vessel == null)
                {
                    StreamWriter sw = File.AppendText(@"C:\Project\Temp\VoyageMasterAgressoError.csv");
                    sw.WriteLine("vessel is null : " + DateTime.Now.ToString() + ", " + lines);
                    sw.Close();
                    continue;
                }
                int resut = vmSvc.GetSoftmarVoyageMasterByVesselNameAndVoyage(vessel.vessel_name, voyage);
                if (resut == 0)
                {
                    StreamWriter sw = File.AppendText(@"C:\Project\Temp\VoyageMasterAgressoError.csv");
                    sw.WriteLine("vessel is not found in softmar : " + DateTime.Now.ToString() + ", " + lines);
                    sw.Close();
                    continue;
                }
                else
                {
                    StreamWriter sw = File.AppendText(@"C:\Project\Temp\VoyageMasterAgressoError.csv");
                    sw.WriteLine("Found in softmar : " + resut + ", " + DateTime.Now.ToString() + ", " + lines);
                    sw.Close();
                }
            }
        }

        private static void ExecuteBunkerInventory()
        {
            //BunkerInventoryService bunkerINvService = new BunkerInventoryService();
            //bunkerINvService.ImportAllDataSoftmarToMiddleLayer();
            //bunkerINvService.ExecuteInterfaceIWTotalConsumption();
            //bunkerINvService.ExecuteInterfaceIWBunkerOnDelivery();
            //bunkerINvService.ExecuteInterfaceIRBunkerOnReDelivery();
            //ConfigurationSettingService confService = new ConfigurationSettingService();
            //ConfigurationSetting cs = confService.GetConfigurationSetting("BunkerInventory");
            //cs.val_10 = DateTime.Now.ToString("dd-MMM-yyyy HH:mm:ss");
            //confService.UpdateConfiguration(cs);
        }

        private static void ImportBunkerToProduction()
        {
            //ChargeTypeAccountMappingService csv = new ChargeTypeAccountMappingService();
            agldimvalueService aglService = new agldimvalueService();
            VesselService vesselService = new VesselService();
            ProfitCentreService pcService = new ProfitCentreService();
            VoyageMasterService vmService = new VoyageMasterService();
            ud_voyageimp_Service ud_voyageimp_Svc = new ud_voyageimp_Service();
            BunkerInventoryService biService = new BunkerInventoryService();
            // vmService.ImportAllDataSoftmarToMiddleLayer();

            // StreamReader sr = new StreamReader(@"C:\Project\Temp\VoyageProductionB.csv");
            StreamReader sr = new StreamReader(@"C:\Project\Temp\VoyageProductionE.csv");
            int i = 0;
            IList vesselNotfound = new ArrayList();
            while (!sr.EndOfStream)
            {
                String lines = sr.ReadLine();
                string[] line = lines.Split(',');
                string voyageCode = line[0];
                string vesselName = line[1];
                string client = line[2];
                string voyage = line[3];
                string vesseldimval = line[4];
                string description = line[5];

                //agresso checkin --- OK
                agldimvalue vesselagl = aglService.GetAglDimValueVesselByDimValueAndClient(vesseldimval, client);
                agldimvalue costcentre = null;
                agldimvalue profitcentre = null;
                ProfitCentre profitCEntreMap = null;

                if (vesselagl == null)
                {
                    StreamWriter sw = File.AppendText(@"C:\Project\Temp\VoyageProductionE_migrationError.csv");
                    sw.WriteLine("vesselagl is null : " + DateTime.Now.ToString() + " " + lines);
                    sw.Close();
                    continue;
                }

                else
                {
                    costcentre = aglService.GetAglDimValueCostCentreByDimVal(vesselagl.rel_value);
                    if (costcentre == null)
                    {
                        StreamWriter sw = File.AppendText(@"C:\Project\Temp\VoyageProductionE_migrationError.csv");
                        sw.WriteLine("costcentre is null : " + DateTime.Now.ToString() + " " + lines);
                        sw.Close();
                        continue;
                    }
                    else
                    {
                        profitcentre = aglService.GetAglDimValueProfitCentreByDimValue(costcentre.rel_value);
                        if (profitcentre == null)
                        {
                            StreamWriter sw = File.AppendText(@"C:\Project\Temp\VoyageProductionE_migrationError.csv");
                            sw.WriteLine("profitcentre is null : " + DateTime.Now.ToString() + " " + lines);
                            sw.Close();
                            continue;
                        }
                        else
                        {
                            profitCEntreMap = pcService.GetProfitCentreByDimValue(profitcentre.dim_value);
                        }
                    }
                }


                Vessel vMapList = vesselService.GetAllVesselMappingByDimValueAndClient(vesseldimval, client.Trim());
                if (vMapList == null)
                {
                    vesselNotfound.Add(vesselName);
                    StreamWriter sw = File.AppendText(@"C:\Project\Temp\VoyageProductionE_migrationError.csv");
                    sw.WriteLine("vesselNotfound : " + DateTime.Now.ToString() + " " + lines);
                    sw.Close();
                    continue;
                }
                else
                {
                    IList voyageMasterList = vmService.GetVoyageMasterByVesselNameAndVoyageAndProfitCentre(vMapList.vessel_name.Trim(), voyage, profitCEntreMap.full_name.Trim());
                    if (voyageMasterList.Count == 0)
                    {
                        voyageMasterList = vmService.GetVoyageMasterByVesselNameAndVoyageAndProfitCentre(vMapList.agr_description.Trim(), voyage, profitCEntreMap.full_name.Trim());
                        if (voyageMasterList.Count == 0)
                        {
                            StreamWriter sw = File.AppendText(@"C:\Project\Temp\VoyageProductionE_migrationError.csv");
                            sw.WriteLine("voyageMasterList count 0 : " + DateTime.Now.ToString() + " " + lines);
                            sw.Close();
                            continue;
                        }
                    }
                    if (voyageMasterList.Count > 0)
                    {
                        if (vesselagl != null && costcentre != null && profitcentre != null)
                        {
                            int result = biService.ImportAllDataSoftmarToMiddleLayerMigration(voyage, vMapList.vessel_name, description);
                            if (result == 0)
                            {
                                result = biService.ImportAllDataSoftmarToMiddleLayerMigration(voyage, vesselName.Trim(), description);
                                if (result == 0)
                                {
                                    StreamWriter sw = File.AppendText(@"C:\Project\Temp\VoyageProductionE_migrationError.csv");
                                    sw.WriteLine("BI count 0 :, " + DateTime.Now.ToString() + " " + lines);
                                    sw.Close();
                                    continue;
                                }
                                else
                                {

                                    StreamWriter sw = File.AppendText(@"C:\Project\Temp\VoyageProductionE_migrationError.csv");
                                    sw.WriteLine("BI sucessfuly insert :, " + DateTime.Now.ToString() + " " + lines);
                                    sw.Close();
                                }
                            }
                            else
                            {

                                StreamWriter sw = File.AppendText(@"C:\Project\Temp\VoyageProductionE_migrationError.csv");
                                sw.WriteLine("BI sucessfuly insert :, " + DateTime.Now.ToString() + " " + lines);
                                sw.Close();
                            }
                        }
                    }
                }
                i++;
            }
        }

        private static void ImportVoyageToProduction()
        {

            //ChargeTypeAccountMappingService csv = new ChargeTypeAccountMappingService();
            agldimvalueService aglService = new agldimvalueService();
            VesselService vesselService = new VesselService();
            ProfitCentreService pcService = new ProfitCentreService();
            VoyageMasterService vmService = new VoyageMasterService();
            ud_voyageimp_Service ud_voyageimp_Svc = new ud_voyageimp_Service();
            // vmService.ImportAllDataSoftmarToMiddleLayer();

           // StreamReader sr = new StreamReader(@"C:\Project\Temp\VoyageProductionB.csv");
            StreamReader sr = new StreamReader(@"C:\Project\Temp\VoyageProductionC.csv");
            int i = 0;
            IList vesselNotfound = new ArrayList();
            while (!sr.EndOfStream)
            {
                String lines = sr.ReadLine();
                string[] line = lines.Split(',');
                string voyageCode = line[0];
                string vesselName = line[1];
                string client = line[2];
                string voyage = line[3];
                string vesseldimval = line[4];

                //agresso checkin --- OK
                agldimvalue vesselagl = aglService.GetAglDimValueVesselByDimValueAndClient(vesseldimval, client);
                agldimvalue costcentre = null;
                agldimvalue profitcentre = null;
                ProfitCentre profitCEntreMap = null;

                if (vesselagl == null)
                {
                    StreamWriter sw = File.AppendText(@"C:\Project\Temp\VoyageProductionC_error.csv");
                    sw.WriteLine("vesselagl is null : "+DateTime.Now.ToString() + " " + lines);
                    sw.Close();
                    continue;
                }

                else
                {
                    costcentre = aglService.GetAglDimValueCostCentreByDimVal(vesselagl.rel_value);
                    if (costcentre == null)
                    {
                        StreamWriter sw = File.AppendText(@"C:\Project\Temp\VoyageProductionC_error.csv");
                        sw.WriteLine("costcentre is null : " + DateTime.Now.ToString() + " " + lines);
                        sw.Close();
                        continue;
                    }
                    else
                    {
                        profitcentre = aglService.GetAglDimValueProfitCentreByDimValue(costcentre.rel_value);
                        if (profitcentre == null)
                        {
                            StreamWriter sw = File.AppendText(@"C:\Project\Temp\VoyageProductionC_error.csv");
                            sw.WriteLine("profitcentre is null : " + DateTime.Now.ToString() + " " + lines);
                            sw.Close();
                            continue;
                        }
                        else
                        {
                            profitCEntreMap = pcService.GetProfitCentreByDimValue(profitcentre.dim_value);
                        }
                    }
                }


                Vessel vMapList = vesselService.GetAllVesselMappingByDimValueAndClient(vesseldimval, client.Trim());
                if (vMapList == null)
                {
                    vesselNotfound.Add(vesselName);
                    StreamWriter sw = File.AppendText(@"C:\Project\Temp\VoyageProductionC_error.csv");
                    sw.WriteLine("vesselNotfound : " + DateTime.Now.ToString() + " " + lines);
                    sw.Close();
                    continue;
                }
                else
                {
                    IList voyageMasterList = vmService.GetVoyageMasterByVesselNameAndVoyageAndProfitCentre(vMapList.vessel_name.Trim(), voyage, profitCEntreMap.full_name.Trim());
                    if (voyageMasterList.Count == 0)
                    {
                        voyageMasterList = vmService.GetVoyageMasterByVesselNameAndVoyageAndProfitCentre(vMapList.agr_description.Trim(), voyage, profitCEntreMap.full_name.Trim());
                        if (voyageMasterList.Count == 0)
                        {
                            StreamWriter sw = File.AppendText(@"C:\Project\Temp\VoyageProductionC_error.csv");
                            sw.WriteLine("voyageMasterList count 0 : " + DateTime.Now.ToString() + " " + lines);
                            sw.Close();
                            continue;
                        }
                    }
                     if (voyageMasterList.Count > 0)
                    {
                        if (vesselagl != null && costcentre != null && profitcentre != null)
                        {
                            //VoyageMaster vm = (VoyageMaster)voyageMasterList[0];
                            //ud_voyageimp udv = new ud_voyageimp(profitcentre.client, vesseldimval, vm);
                            //ud_voyageimp_Svc.Insert(udv);
                            //vmService.UpdateFlag(vm.i_voyage_master_id, udv.recid.ToString(), "1");

                            //StreamWriter sw = File.AppendText(@"C:\Project\Temp\VoyageProductionC_error.csv");
                            //sw.WriteLine("Success fully pushed in : " + DateTime.Now.ToString() + " " + lines);
                            //sw.Close();
                            //sw.Close();
                        }
                    }
                }
                i++;
            }
        }

        private static void SupplierCustomerCheck()
        {
            SupplierCustomerService sc = new SupplierCustomerService();
            StreamReader sr = new StreamReader(@"C:\Project\Temp\SupplierCustomerProductionMiddleLayerc.csv");
            while (!sr.EndOfStream)
            {
                string[] lines = sr.ReadLine().Split(',');
                string company = lines[0].Replace(';', ',');
                string supName = lines[1].Replace(';', ',');
                string cusName = lines[2].Replace(';', ',');
                SupplierCustomer supcus = sc.GetSupplierMapping(company.Trim());
                //supcus.agr_supp_apar_name = supName;
                //supcus.agr_cust_apar_name = supName;
                //sc.UpdateSupplierCustomerMapping(supcus);
                //if (company. == null)
                //{
                //    listNull.Add(supp);
                //}
            }
        }
        private static void CheckAgressoProductionSupplier()
        {
            asuheaderService svc = new asuheaderService();
            StreamReader sr = new StreamReader(@"C:\Project\Temp\Supplier_production.csv");
            ArrayList listNull = new ArrayList();
            while (!sr.EndOfStream)
            {
                String lines = sr.ReadLine();
                string[] line = lines.Split(',');
                string apar_id = line[0];
                asuheader supp = svc.GetSupplierByCode(apar_id);
                if (supp == null)
                {
                    listNull.Add(supp);
                }
            }
        }
    }
}
